package xyz.quanguanjia.masterboy;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import androidx.fragment.app.FragmentManager;

import android.graphics.Color;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

import xyz.quanguanjia.masterboy.base.BaseFragment;
import xyz.quanguanjia.masterboy.home.HomeFragment;
import xyz.quanguanjia.masterboy.me.MineFragment;
import xyz.quanguanjia.masterboy.message.MessageFragment;

public class MainActivity extends AppCompatActivity {
    public static final int LOGIN_REQUEST_CODE = 100; // 请求码

    private ArrayList<BaseFragment> fragments;
    private TabLayout tabLayout;
    private ViewPagerAdapter mAdapter;
    private ViewPager2 pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        fragments = new ArrayList<BaseFragment>();
        HomeFragment latestFragment = new HomeFragment();
        MessageFragment messageFragment = new MessageFragment();
        MineFragment mineFragment = new MineFragment();
        fragments.add(latestFragment);
        fragments.add(messageFragment);
        fragments.add(mineFragment);

        pager = (ViewPager2) findViewById(R.id.main_pager);
        tabLayout = findViewById(R.id.tabLayout);
        tabLayout.setBackgroundColor(Color.WHITE);
        tabLayout.setTabTextColors(Color.parseColor("#333333"), Color.parseColor("#4273d0"));

        pager.setOffscreenPageLimit(3);

        FragmentManager fragmentManager = getSupportFragmentManager();
        mAdapter = new ViewPagerAdapter(fragmentManager, fragments, getLifecycle());
        pager.setAdapter(mAdapter);

        pager.registerOnPageChangeCallback(new ViewPager2PageChangeListener());

        ArrayList<TabLayoutTabListener.TabIdModel> ids = TabLayoutTabListener.TabIdModel.idsWithFragments(fragments);

        tabLayout.addOnTabSelectedListener(new TabLayoutTabListener(ids));

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, pager,
                (tab, position) -> {
                    // 设置 TabLayout 的标签名称
                    tab.setText(fragments.get(position).fragmentTitle());
                    if (position == 0) {
                        tab.setCustomView(mAdapter.getTabView(0, BaseFragment.HIGHLIGHT));
                    } else if (position == 1) {
                        tab.setCustomView(mAdapter.getTabView(1, BaseFragment.NORMAL));
                    } else {
                        tab.setCustomView(mAdapter.getTabView(position, BaseFragment.NORMAL));
                    }
                });
        tabLayoutMediator.attach();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


}